package com.smart.primary.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MoneyUtil {
	public static DecimalFormat fmt = new DecimalFormat("#.##");


    /** 
     * 格式化金额
     */  
    public static String fmt(Object value){
        return fmt.format(getVal(value));
    }
    /** 
     * 金额相加
     */  

    public static String add(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        return fmt(value1.add(value2));
    }
    public static BigDecimal addToBig(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        return value1.add(value2);
    }
    public static BigDecimal addd(Object...val){
        BigDecimal value=BigDecimal.ZERO;
        for (Object obj: val) {
            value=value.add(getVal(obj));
        }
        return value;
    }


    /**
     * 金额相减
     */

    public static String sub(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        return fmt(value1.subtract(value2));
    }
    public static BigDecimal subToBig(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        return value1.subtract(value2);
    }
    /**
     * 金额相乘
     */
    public static String mul(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        return fmt(value1.multiply(value2));
    }
    public static BigDecimal mulToBig(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        return value1.multiply(value2);
    }
    /**
     * 金额相除 <br/>
     */
    public static String div(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        // 四舍五入的操作 保留两位小数
        return fmt(value1.divide(value2,2,BigDecimal.ROUND_HALF_UP));
    }
    public static BigDecimal divToBig(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        // 四舍五入的操作 保留两位小数
        return value1.divide(value2,2,BigDecimal.ROUND_HALF_UP);
    }



    /**
     * 值比较大小
     */
    public static Boolean comp(Object val,Object value){
        BigDecimal value1=getVal(val);
        BigDecimal value2=getVal(value);
        int result = value1.compareTo(value2);
        if(result >= 0){
            return true;
        }else{
            return false;
        }
    }

    public  static BigDecimal getVal(Object obj){
        try {
           return new BigDecimal(obj.toString());
        }catch (Exception e){
           return new BigDecimal(0);
        }
    }

    public  static BigDecimal getVal2(Object obj){
        try {
            return new BigDecimal(obj.toString());
        }catch (Exception e){
            return null;
        }
    }

    /** 
     * 给金额加逗号切割 
     * @param str 
     * @return 
     */  
    public static String addComma(String str) {  
        try {  
            String banNum = "";  
            if (str.contains(".")) {  
                String[] arr = str.split("\\.");  
                if (arr.length == 2) {  
                    str = arr[0];  
                    banNum = "." + arr[1];  
                }  
            }  
            // 将传进数字反转  
            String reverseStr = new StringBuilder(str).reverse().toString();  
            String strTemp = "";  
            for (int i = 0; i < reverseStr.length(); i++) {  
                if (i * 3 + 3 > reverseStr.length()) {  
                    strTemp += reverseStr.substring(i * 3, reverseStr.length());  
                    break;  
                }  
                strTemp += reverseStr.substring(i * 3, i * 3 + 3) + ",";  
            }  
            // 将[789,456,] 中最后一个[,]去除  
            if (strTemp.endsWith(",")) {  
                strTemp = strTemp.substring(0, strTemp.length() - 1);  
            }  
            // 将数字重新反转  
            String resultStr = new StringBuilder(strTemp).reverse().toString();  
            resultStr += banNum;  
            return resultStr;  
        }catch(Exception e){  
            return str;  
        }  
  
    }

}